package com.ricchieri.manage.bean;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.ricchieri.common.BusinessSettings;
import com.ricchieri.filter.CharterFilter;
import com.ricchieri.util.FacesUtil;

@ManagedBean(name = "editCharterMB")
@ViewScoped
public class EditCharterManageBean extends CommonCharterManageBean {

	private static final long serialVersionUID = -8201281010249836515L;
	
	private CharterFilter charterFilter;
	
	@PostConstruct
	public void init() {
		super.init();
		charterFilter = (CharterFilter) FacesUtil.getParametersFlash(BusinessSettings.KEY_FILTER);
	}
	
	public String getSelectedCharted() {
		if (selectedCharted == null) {
			selectedCharted = String.valueOf(getCharter().getCharterType().getId());
		}
		return selectedCharted;
	}
	
	@Override
	public String save() {
		if(this.validateForm()) {
			getCharter().setCharterType(obtainCharterType(this.getSelectedCharted()));
			charterServices.updateCharter(getCharter());
			FacesUtil.addParametersFlash(BusinessSettings.KEY_CHARTER, getCharter());
			return BusinessSettings.VIEW_DETAILS;
		}
		return "";
	}

	@Override
	public Boolean isRequiredCharter() {
		return Boolean.TRUE;
	}
	
	public String doBackToSearchCharter() {
		FacesUtil.addParametersFlash(BusinessSettings.KEY_FILTER, charterFilter);
		
		return BusinessSettings.SEARCH_CHARTER;
	}
}
